<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * index.php
 * Began: Tue December 24 2002
 * 
 * $Id: index.php 541 2008-05-20 06:56:16Z rspeicher $
 * 
 ******************************/
 
define('EQDKP_INC', true);
define('IN_ADMIN', true);
$eqdkp_root_path = './../';
include_once($eqdkp_root_path . 'common.php');

// Start session management
$user->start();
$user->setup();
$user->check_auth('a_');

if ( !isset($_GET['pane']) )
{
    $page_title = $user->lang['admin_index_title'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title><?php echo $page_title ?></title>
</head>

<frameset cols="175,100%" rows="*" border="0" framespacing="0" frameborder="NO">
  <frame src="./index.php?pane=left" name="menu" scrolling="auto">
  <frame src="./index.php?pane=right" name="main" scrolling="auto">
</frameset>

<noframes>
<body bgcolor="#FFFFFF">
Your browser doesn't support frames, sorry.
</body>
</noframes>

</html>
<?php
}                                                                       
//
// Menu
//
elseif ( $_GET['pane'] == 'left' )
{
    $tpl->assign_vars(array(
        'L_ADMINISTRATION' => $user->lang['administration'],
        'L_ADMIN_INDEX' => $user->lang['admin_index'],
        'L_EQDKP_INDEX' => $user->lang['eqdkp_index'],
        'L_EVENTS' => $user->lang['events'],
        'L_GROUP_ADJUSTMENTS' => $user->lang['group_adjustments'],
        'L_INDIVIDUAL_ADJUSTMENTS' => $user->lang['individual_adjustments'],
        'L_ITEMS' => $user->lang['items'],
        'L_NEWS' => $user->lang['news'],
        'L_RAIDS' => $user->lang['raids'],
        'L_TURNINS' => $user->lang['turn_ins'],
        'L_GENERAL_ADMIN' => $user->lang['general_admin'],
        'L_CONFIGURATION' => $user->lang['configuration'],
        'L_BACKUP_DATABSE' => $user->lang['backup_database'],
        'L_MANAGE_MEMBERS' => $user->lang['manage_members'],
        'L_MANAGE_PLUGINS' => $user->lang['manage_plugins'],
        'L_MANAGE_USERS' => $user->lang['manage_users'],
        'L_VIEW_LOGS' => $user->lang['view_logs'],
        'L_STYLES' => $user->lang['styles'],
        'L_CREATE' => $user->lang['create'],
        'L_MANAGE' => $user->lang['manage'],
        
        'L_ADD' => $user->lang['add'],
        'L_LIST' => $user->lang['list'])
    );
    
    $gen_simple_header = true;
    $page_title = '';
    include_once($eqdkp_root_path . 'includes/page_header.php');
    
    $tpl->set_filenames(array(
        'body' => 'admin/menu.html')
    );
    
    include_once($eqdkp_root_path . 'includes/page_tail.php');
}
//
// Admin Index
//
else
{
    $days = ((time() - $eqdkp->config['eqdkp_start']) / 86400);

    $total_members_inactive = $db->query_first('SELECT count(*) FROM ' . MEMBERS_TABLE . " where member_status='0'");
    $total_members_active = $db->query_first('SELECT count(*) FROM ' . MEMBERS_TABLE . " where member_status='1'");
    $total_members = $total_members_active . ' / ' . $total_members_inactive;
    
    $total_raids = $db->query_first('SELECT count(*) FROM ' . RAIDS_TABLE);
    $raids_per_day = sprintf("%.2f", ($total_raids / $days));
    
    $total_items = $db->query_first('SELECT count(*) FROM ' . ITEMS_TABLE);
    $items_per_day = sprintf("%.2f", ($total_items / $days));
    
    $total_logs = $db->query_first('SELECT count(*) FROM ' . LOGS_TABLE);
    
    if ( $raids_per_day > $total_raids )
    {
        $raids_per_day = $total_raids;
    }
    if ( $items_per_day > $total_items )
    {
        $items_per_day = $total_items;
    }
    
    // DB Size - MySQL Only
    if ( DBTYPE == 'mysql' )
    {
        $result = $db->query('SELECT VERSION() AS mysql_version');
        
        if ( $row = $db->fetch_record($result) )
        {
            $version = $row['mysql_version'];
            
            if ( preg_match('/^(3\.23|4\.)/', $version) )
            {
                $db_name = ( preg_match('/^(3\.23\.[6-9])|(3\.23\.[1-9][1-9])|(4\.)/', $version) ) ? "`$dbname`" : $dbname;
                
                $sql = 'SHOW TABLE STATUS
                        FROM ' . $db_name;
                $result = $db->query($sql);
                
                $dbsize = 0;
                while ( $row = $db->fetch_record($result) )
                {
                    if ( $row['Type'] != 'MRG_MyISAM' )
                    {
                        $dbsize += $row['Data_length'] + $row['Index_length'];
                    }
                }
            }
            else
            {
                $dbsize = $user->lang['not_available'];
            }
        }
        else
        {
            $dbsize = $user->lang['not_available'];
        }
    }
    else
    {
        $dbsize = $user->lang['not_available'];
    }
    
    if ( is_int($dbsize) )
    {
        $dbsize = ( $dbsize >= 1048576 ) ? sprintf('%.2f MB', ($dbsize / 1048576)) : (($dbsize >= 1024) ? sprintf('%.2f KB', ($dbsize / 1024)) : sprintf('%.2f Bytes', $dbsize));
    }
    
    //
    // Who's Online
    //
    $sql = 'SELECT s.*, u.username
            FROM ( ' . SESSIONS_TABLE . ' s
            LEFT JOIN ' . USERS_TABLE . ' u
            ON u.user_id = s.session_user_id )
            ORDER BY u.username';
    $result = $db->query($sql);
    while ( $row = $db->fetch_record($result) )
    {
        $session_page = preg_replace("/(".URI_SESSION."=)[0-9A-Za-z]{32}/", '\\1', $row['session_page']);
        $session_page = preg_replace("/.php([^\?])/", '.php?\\1', $session_page);
        $session_page = str_replace($eqdkp->config['server_path'], '', $session_page);
        
        $tpl->assign_block_vars('online_row', array(
            'ROW_CLASS' => $eqdkp->switch_row_class(),
            'USERNAME' => ( !empty($row['username']) ) ? $row['username'] : $user->lang['anonymous'],
            'LOGIN' => date($user->style['date_time'], $row['session_start']),
            'LAST_UPDATE' => date($user->style['date_time'], $row['session_current']),
            'LOCATION' => $session_page,
            'IP_ADDRESS' => $row['session_ip'])
        );
    }
    $online_count = $db->num_rows($result);
    
    // Log Actions
    $s_logs = false;
    if ( $total_logs > 0 )
    {
        $sql = 'SELECT l.*, u.username 
                FROM ' . LOGS_TABLE . ' l, ' . USERS_TABLE . ' u
                WHERE u.user_id=l.admin_id
                ORDER BY l.log_date DESC
                LIMIT 10';
        $result = $db->query($sql);
        while ( $row = $db->fetch_record($result) )
        {
            eval($row['log_action']);
            
            switch ( $row['log_type'] )
            {
                case '{L_ACTION_EVENT_ADDED}':
                    $logline = sprintf($user->lang['vlog_event_added'], $row['username'], $log_action['{L_NAME}'], $log_action['{L_VALUE}']);
                    break;
                case '{L_ACTION_EVENT_UPDATED}':
                    $logline = sprintf($user->lang['vlog_event_updated'], $row['username'], $log_action['{L_NAME_BEFORE}']);
                    break;
                case '{L_ACTION_EVENT_DELETED}':
                    $logline = sprintf($user->lang['vlog_event_deleted'], $row['username'], $log_action['{L_NAME}']);
                    break;
                case '{L_ACTION_GROUPADJ_ADDED}':
                    $logline = sprintf($user->lang['vlog_groupadj_added'], $row['username'], $log_action['{L_ADJUSTMENT}']);
                    break;
                case '{L_ACTION_GROUPADJ_UPDATED}':
                    $logline = sprintf($user->lang['vlog_groupadj_updated'], $row['username'], $log_action['{L_ADJUSTMENT_BEFORE}']);
                    break;
                case '{L_ACTION_GROUPADJ_DELETED}':
                    $logline = sprintf($user->lang['vlog_groupadj_deleted'], $row['username'], $log_action['{L_ADJUSTMENT}']);
                    break;
                case '{L_ACTION_INDIVADJ_ADDED}':
                    $logline = sprintf($user->lang['vlog_indivadj_added'], $row['username'], $log_action['{L_ADJUSTMENT}'], count(explode(', ', $log_action['{L_MEMBERS}'])));
                    break;
                case '{L_ACTION_INDIVADJ_UPDATED}':
                    $logline = sprintf($user->lang['vlog_indivadj_updated'], $row['username'], $log_action['{L_ADJUSTMENT_BEFORE}'], $log_action['{L_MEMBERS_BEFORE}']);
                    break;
                case '{L_ACTION_INDIVADJ_DELETED}':
                    $logline = sprintf($user->lang['vlog_indivadj_deleted'], $row['username'], $log_action['{L_ADJUSTMENT}'], $log_action['{L_MEMBERS}']);
                    break;
                case '{L_ACTION_ITEM_ADDED}':
                    $logline = sprintf($user->lang['vlog_item_added'], $row['username'], $log_action['{L_NAME}'], count(explode(', ', $log_action['{L_BUYERS}'])), $log_action['{L_VALUE}']);
                    break;
                case '{L_ACTION_ITEM_UPDATED}':
                    $logline = sprintf($user->lang['vlog_item_updated'], $row['username'], $log_action['{L_NAME_BEFORE}'], count(explode(', ', $log_action['{L_BUYERS_BEFORE}'])));
                    break;
                case '{L_ACTION_ITEM_DELETED}':
                    $logline = sprintf($user->lang['vlog_item_deleted'], $row['username'], $log_action['{L_NAME}'], count(explode(', ', $log_action['{L_BUYERS}'])));
                    break;
                case '{L_ACTION_MEMBER_ADDED}':
                    $logline = sprintf($user->lang['vlog_member_added'], $row['username'], $log_action['{L_NAME}']);
                    break;
                case '{L_ACTION_MEMBER_UPDATED}':
                    $logline = sprintf($user->lang['vlog_member_updated'], $row['username'], $log_action['{L_NAME_BEFORE}']);
                    break;
                case '{L_ACTION_MEMBER_DELETED}':
                    $logline = sprintf($user->lang['vlog_member_deleted'], $row['username'], $log_action['{L_NAME}']);
                    break;
                case '{L_ACTION_NEWS_ADDED}':
                    $logline = sprintf($user->lang['vlog_news_added'], $row['username'], $log_action['{L_HEADLINE}']);
                    break;
                case '{L_ACTION_NEWS_UPDATED}':
                    $logline = sprintf($user->lang['vlog_news_updated'], $row['username'], $log_action['{L_HEADLINE_BEFORE}']);
                    break;
                case '{L_ACTION_NEWS_DELETED}':
                    $logline = sprintf($user->lang['vlog_news_deleted'], $row['username'], $log_action['{L_HEADLINE}']);
                    break;
                case '{L_ACTION_RAID_ADDED}':
                    $logline = sprintf($user->lang['vlog_raid_added'], $row['username'], $log_action['{L_EVENT}']);
                    break;
                case '{L_ACTION_RAID_UPDATED}':
                    $logline = sprintf($user->lang['vlog_raid_updated'], $row['username'], $log_action['{L_EVENT_BEFORE}']);
                    break;
                case '{L_ACTION_RAID_DELETED}':
                    $logline = sprintf($user->lang['vlog_raid_deleted'], $row['username'], $log_action['{L_EVENT}']);
                    break;
                case '{L_ACTION_TURNIN_ADDED}':
                    break;
            }
            unset($log_action);
            
            // Show the log if we have a valid line for it
            if ( isset($logline) )
            {
                $tpl->assign_block_vars('actions_row', array(
                    'ROW_CLASS' => $eqdkp->switch_row_class(),
                    'U_VIEW_LOG' => 'logs.php?' . URI_LOG . '='.$row['log_id'],
                    'ACTION' => stripslashes($logline))
                );
            }
        }
        $db->free_result($result);
        
        $s_logs = true;
    }
    
    $tpl->assign_vars(array(
        'S_LOGS' => $s_logs,
        
        'L_STATISTICS' => $user->lang['statistics'],
        'L_NUMBER_OF_MEMBERS' => $user->lang['number_of_members'],
        'L_NUMBER_OF_RAIDS' => $user->lang['number_of_raids'],
        'L_NUMBER_OF_ITEMS' => $user->lang['number_of_items'],
        'L_DATABASE_SIZE' => $user->lang['database_size'],
        'L_NUMBER_OF_LOGS' => $user->lang['number_of_logs'],
        'L_RAIDS_PER_DAY' => $user->lang['raids_per_day'],
        'L_ITEMS_PER_DAY' => $user->lang['items_per_day'],
        'L_EQDKP_STARTED' => $user->lang['eqdkp_started'],
        
        'NUMBER_OF_MEMBERS' => $total_members,
        'NUMBER_OF_RAIDS' => $total_raids,
        'NUMBER_OF_ITEMS' => $total_items,
        'DATABASE_SIZE' => $dbsize,
        'NUMBER_OF_LOGS' => $total_logs,
        'RAIDS_PER_DAY' => $raids_per_day,
        'ITEMS_PER_DAY' => $items_per_day,
        'EQDKP_STARTED' => date($user->style['date_time'], $eqdkp->config['eqdkp_start']),
        
        'L_WHO_ONLINE' => $user->lang['who_online'],
        'L_USERNAME' => $user->lang['username'],
        'L_LOGIN' => $user->lang['login'],
        'L_LAST_UPDATE' => $user->lang['last_update'],
        'L_LOCATION' => $user->lang['location'],
        'L_IP_ADDRESS' => $user->lang['ip_address'],
        
        'L_NEW_ACTIONS' => $user->lang['new_actions'],
        
        'ONLINE_FOOTCOUNT' => sprintf($user->lang['online_footcount'], $online_count))
    );
    
    $page_title = '';
    include_once($eqdkp_root_path . 'includes/page_header.php');
    
    $tpl->set_filenames(array(
        'body' => 'admin/admin_index.html')
    );
    
    include_once($eqdkp_root_path . 'includes/page_tail.php');
}
?>